package cn.chint.manage.dao.impl;



import java.math.BigInteger;
import java.util.List;

import cn.chint.po.SysUser;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate4.HibernateCallback;
import org.springframework.orm.hibernate4.HibernateTemplate;
import org.springframework.stereotype.Repository;


import cn.chint.bo.UserPages;
import cn.chint.bo.Userbo;
import cn.chint.manage.dao.UserDao;
import cn.chint.po.Orders;
import cn.chint.po.User;
@Repository
public class UserDaoImpl implements UserDao{

	@Autowired
	HibernateTemplate hibernateTemplate;

	@Override
	public void getIns(User user) {
		this.hibernateTemplate.save(user);
	}
	
	@Override
	public void saveOrder(Orders order){
		this.hibernateTemplate.save(order);
	}




	@Override
	public List<User> getSel(final String wxOpenId) {
		@SuppressWarnings({ "unchecked", "rawtypes" })
		List<User> list = (List<User>) this.hibernateTemplate.execute(new HibernateCallback() {
			@Override
			public Object doInHibernate(Session session)throws HibernateException {
				return session.createQuery("from  User u where u.wxOpenId=:wxOpenId")
						.setString("wxOpenId", wxOpenId).list();
				
			}
		});
		return list;
	}


	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public void updateUserInfo(final String wxOpenId,final String token) {
		// TODO Auto-generated method stub
		int num  = (int) this.hibernateTemplate.execute(new HibernateCallback() {
			@Override
			 public Object doInHibernate(Session session)throws HibernateException {
				
				return session.createQuery("update User u set u.token =:token where u.wxOpenId=:wxOpenId")
						.setString("token",token).setString("wxOpenId", wxOpenId).executeUpdate();
			}
		});
	    System.out.println("更新行数量 :"+num);
	}

	@Override
	public void getUpd(String wxOpenId, String token) {
		// TODO Auto-generated method stub
		
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public UserPages Focus_user(final int page,final int size) {
		List<Userbo> ls = (List<Userbo>) this.hibernateTemplate.execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException {
				return session.createQuery("FROM User ").setFirstResult((page-1)*size)
						.setMaxResults(size).list();
						}
		});
		
		BigInteger count = (BigInteger) this.hibernateTemplate.execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException {
				return session.createSQLQuery("select count(*) from t_user").uniqueResult();
			}
		});
		
		UserPages userPages = new UserPages();
		userPages.setList(ls);
		userPages.setUser_num(count.intValue());
		return userPages;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public UserPages focus(final int page, final int size, final String wx_nickname) {
		BigInteger count = (BigInteger) this.hibernateTemplate.execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException {
				return session.createSQLQuery("select count(*) from t_user where wx_nickname like '%"+wx_nickname+"%'").uniqueResult();
			}
		});

		final int cou=count.intValue();
		List<Userbo> ls = (List<Userbo>) this.hibernateTemplate.execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException {
				int page1=page;
				if(cou/size<page){
					page1=cou/size+1;
				}
				return session.createQuery(" from User where wx_nickname like:wx_nickname").setFirstResult((page1-1)*size)
						.setMaxResults(size).setString("wx_nickname", "%"+wx_nickname+"%").list();
						}
		});
		

		
		UserPages userPages = new UserPages();
		userPages.setList(ls);
		userPages.setUser_num(count.intValue());
		return userPages;
	}

}
